﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Data;

namespace Top.PointStore.Business
{
    /*功能说明:规则与城市操作管理类
    *版本:
    *参与人:王士锋
    *修改日期:2015.3.4
    */
    /// <summary>规则与城市
    /// </summary>
    public static class RuleToCityManager
    {
        /// <summary>添加
        /// <para>添加成功返回大于0</para>
        /// <para>0:添加失败</para>
        /// <para>-1:kid有误</para>
        /// <para>-2:商品id有误</para>
        /// <para>-3:规则id有误</para>
        /// <para>-4:城市id有误</para>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static async Task<int> Add(T_RuleToCity model)
        {
            var checkRes = CheckingT_RuleToCity(model);
            if (checkRes < 0) return checkRes;
            using (var db = new DbTour())
            {
                const string sql = "insert into T_RuleToCity(F_RuleKid,F_CityKid) values ({0:F_RuleKid},{0:F_CityKid})";
                return await db.Sql(sql, model).ExecuteNonQuery();
            }
        }

        /// <summary>修改
        /// <para>修改成功返回大于0</para>
        /// <para>0:修改失败</para>
        /// <para>-1:kid有误</para>
        /// <para>-2:商品id有误</para>
        /// <para>-3:规则id有误</para>
        /// <para>-4:城市id有误</para>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static async Task<int> Update(T_RuleToCity model)
        {
            var checkRes = CheckingT_RuleToCity(model);
            if (checkRes < 0) return checkRes;
            using (var db = new DbTour())
            {
                const string sql = "update T_RuleToCity set  F_RuleKid={0:F_RuleKid},F_CityKid ={0:F_CityKid} where Kid={0:Kid}";
                return await db.Sql(sql, model).ExecuteNonQuery();
            }
        }
        /// <summary>
        /// T_RuleToCity模型参数验证
        /// <para>0:验证成功</para>
        /// <para>-1:kid有误</para>
        /// <para>-2:商品id有误</para>
        /// <para>-3:规则id有误</para>
        /// <para>-4:城市id有误</para>
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private static int CheckingT_RuleToCity(T_RuleToCity model)
        {
            if (model.Kid < 0) return -1;
            //if (model.F_ProductKid <= 0) return -2;
            if (model.F_RuleKid <= 0) return -3;
            if (model.F_CityKid <= 0) return -4;
            return 0;
        }
        /// <summary>得到一条记录
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        public static async Task<T_RuleToCity> Get(int kid)
        {
            if (kid <= 0) return new T_RuleToCity();
            const string sql = @"select * from T_RuleToCity where Kid = {0} limit 1";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kid).FirstOrDefault<T_RuleToCity>();
            }
        }

        /// <summary>删除一条记录
        /// </summary>
        /// <param name="kid"></param>
        /// <returns></returns>
        public static async Task<int> Delete(int kid)
        {
            if (kid <= 0) return 0;
            const string sql = @"delete from T_RuleToCity where Kid = {0}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kid).ExecuteNonQuery();
            }
        }

        /// <summary>批量删除
        /// </summary>
        /// <param name="kids"></param>
        /// <returns></returns>
        public static async Task<int> Delete(string kids)
        {
            if (kids == "") return 0;
            const string sql = @"delete from T_RuleToCity where Kid in {0}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, kids).ExecuteNonQuery();
            }
        }

        /// <summary>根据产品编号获取数据列表
        /// </summary>
        /// <param name="productKid"></param>
        /// <returns></returns>
        public static async Task<List<T_PointMain_new>> GetList(int ruleKid)
        {
            if (ruleKid <= 0) return new List<T_PointMain_new>();
            const string sql = @"select * from T_RuleToCity where F_RuleKid = {0}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, ruleKid).ToList<T_PointMain_new>();
            }
        }
    }
}
